package com.epfuse.cloud.gateway.config;

import cn.hutool.core.io.LineHandler;
import com.epfuse.cloud.gateway.handler.ImageCodeHandler;
import com.epfuse.cloud.gateway.handler.TimeHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.*;


/**
 * 路由配置信息 获取验证码
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
@RequiredArgsConstructor
public class RouterFunctionConfiguration {

    private final ImageCodeHandler imageCodeHandler;

    private final TimeHandler timeHandler;

    @Bean
    public RouterFunction<ServerResponse> routerFunction() {
        return RouterFunctions
                .route(RequestPredicates.path("/code").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),
                        imageCodeHandler);
    }

    /**
     * 这个是用来测试练习的
     * @return
     */
    @Bean
    public RouterFunction<ServerResponse> routerTimeFunction() {

        /**
         *  处理数据的逻辑采用了函数式接口
         *
         *  接受一个ServerRequest
         *
         *  返回一个ServerResponse
         *
         */
        return RouterFunctions
                .route(RequestPredicates.path("/time").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), timeHandler::getTime)
                .andRoute(RequestPredicates.path("/date").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), timeHandler::getDate)
                .andRoute(RequestPredicates.path("/times").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),timeHandler::sendTimePerSec)
                ;
    }
}
